﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;

namespace DemoLocalization.Assets.Localization
{
    public class LocalizationManager
    {
        /// <summary>
        /// Occurs when the culture changed.
        /// </summary>
        public static event EventHandler CultureChanged;

        /// <summary>
        /// Gets the supported cultures.
        /// </summary>
        /// <value>The supported cultures.</value>
        public static IList<CultureInfo> SupportedCultures { get; private set; }

        private static ResourceManager DefaultResourceManager { get; set; }

        private static Assembly DefaultResourceAssembly { get; set; }

        private static Dictionary<string, ResourceManager> ResourceManagers = new Dictionary<string, ResourceManager>(StringComparer.OrdinalIgnoreCase);

        /// <summary>
        /// Gets and sets the currently selected culture
        /// </summary>
        public static CultureInfo CurrentUiCulture
        {
            get { return CultureInfo.CurrentUICulture; }
            set
            {
                Thread.CurrentThread.CurrentUICulture = value;

                if (CultureChanged != null)
                {
                    CultureChanged(null, EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Initializes the <see cref="LocalizationManager"/> class.
        /// </summary>
        static LocalizationManager()
        {
            SupportedCultures = new List<CultureInfo>();
        }

        public static void Initialize(Assembly defaultAssembly, ResourceManager defaultResourceManager)
        {
            LocalizationManager.DefaultResourceAssembly = defaultAssembly;
            LocalizationManager.DefaultResourceManager = defaultResourceManager;
            RegisterResourceManager(defaultResourceManager);
        }

        public static void RegisterResourceManager(ResourceManager resourceManager)
        {
            
            
            if (!LocalizationManager.ResourceManagers.ContainsKey(resourceManager.BaseName))
            {
                LocalizationManager.ResourceManagers.Add(resourceManager.BaseName, resourceManager);
            }
        }

        public static void RegisterResourceManager(string baseName, Assembly assembly)
        {            
            var resourceManager = new ResourceManager(baseName, assembly);
            RegisterResourceManager(resourceManager);
        }

        /// <summary>
        /// Retrieves a resource manager for the appropriate ResourceSet
        /// By default the 'global' Resource
        /// </summary>
        /// <param name="resourceSet"></param>
        /// <returns></returns>
        internal static ResourceManager GetResourceManager(string resourceSet)
        {
            if (string.IsNullOrEmpty(resourceSet))
            {
                return LocalizationManager.DefaultResourceManager ?? null;
            }

            if (LocalizationManager.ResourceManagers.ContainsKey(resourceSet))
            {
                return LocalizationManager.ResourceManagers[resourceSet];
            }

            if (LocalizationManager.DefaultResourceAssembly == null)
            {
                return null;
            }

            var resourceManager = new ResourceManager(resourceSet, DefaultResourceAssembly);
            ResourceManagers.Add(resourceSet, resourceManager);
            return resourceManager;
        }


    }
}